﻿using System;
using System.Globalization;
using System.Windows.Data;

namespace AppMachine.Converter {
    /// <summary>
    /// 数字与bool值集合的转换，通常用于单选、复选框
    /// </summary>
    public class Int2BoolListConverter : IValueConverter {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            if(int.TryParse(value?.ToString(), out int tempValue) && int.TryParse(parameter?.ToString(),out int tempParameter)) {
                return tempValue == tempParameter;
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            if (bool.TryParse(value?.ToString(), out bool tempValue)) {
                if (tempValue) {
                    if(int.TryParse(parameter.ToString(), out int tempInt)){
                        return tempInt;
                    }
                }
            }
            return null;
        }
    }
}
